// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "../Components"

AndroidItem {
    id: _root
    width: 720 * viewScale
    height: 1134 * viewScale

    property string lockScreenPath: ""          //ロックスクリーン

    signal clicked(int key)
    signal unlocked()

    Image {
        anchors.fill: parent
        source: _root.lockScreenPath
        clip: true
        fillMode: Image.PreserveAspectCrop
        smooth: true
    }

    MouseArea{
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton

        onPressed: {
            //ほわほわ
            _chevron.start();
        }
    }
    //ハンドル
    ImageButton{
        id: _handle
        x: parent.width / 2 - width / 2
        y: 840 * viewScale
        width: 216 * viewScale
        height: 216 * viewScale
        imageIdle: "../Android/images/ic_lockscreen_handle_normal.png"
        imagePress: "../Android/images/ic_lockscreen_handle_pressed.png"

        onClicked: {
            _root.clicked(1);
        }
        property real r: parent.width / 2 - 100 * viewScale
        property real default_x: 0
        property real default_y: 0
        property real prev_x: 0
        property real prev_y: 0
        property bool isUnlocked: false
        property bool isHit: false      // 現状、３つのマークにあたってるか

        Component.onCompleted: {
            default_x = x;
            default_y = y;
        }

        onPressed: {
            prev_x = mouse.x;
            prev_y = mouse.y;
            _funcItemsAnim.stop();
            _funcItems.opacity = 1.0;
            forcePressed = true;
        }
        onReleased: {
//            x = default_x;
//            y = default_y;

            if(isUnlocked === true){
                unlocked();
            }
            //もどす
            _handleBackAnim.start();
            //外円
            _funcItemsAnim.start();
            //ほわほわ
            _chevron.start();
        }
        onMousePositionChanged: {
            if(pressed){
                var tx = x + mouse.x - prev_x;
                var ty = y + mouse.y - prev_y;
                if(checkInCircle(tx, ty) === true){
                    x += mouse.x - prev_x;
                    y += mouse.y - prev_y;
                }else{
                    x = calcOutCircleX(tx, ty);
                    y = calcOutCircleY(tx, ty);
                }

                tx = x + width / 2;
                ty = y + height / 2;
                if(checkHit(_unlock, tx, ty, isHit) === true){
                    x = _unlock.x;
                    y = _unlock.y;
                    isUnlocked = true;
                    isHit = true;
                }else{
                    if(checkHit(_camera, tx, ty, isHit) === true){
                        x = _camera.x;
                        y = _camera.y;
                        isHit = true;
                    }else if(checkHit(_silent, tx, ty, isHit) === true){
                        x = _silent.x;
                        y = _silent.y;
                        isHit = true;
                    }else{
                        isHit = true;
                    }
                    isUnlocked = false;
                }
            }
        }

        //
        function calcOutCircleX(x, y){
            var dx = x - default_x;
            var dy = y - default_y;
            var rad = Math.atan2(dy, dx);
            return r * Math.cos(rad) + default_x;
        }
        function calcOutCircleY(x, y){
            var dx = x - default_x;
            var dy = y - default_y;
            var rad = Math.atan2(dy, dx);
            return r * Math.sin(rad) + default_y;
        }

        //特定の範囲にいるか
        function checkInCircle(x, y){
            var ret = false;
            if(Math.pow(x - default_x, 2) + Math.pow(y - default_y, 2) < Math.pow(r, 2)){
                //範囲内
                ret = true;
            }
            return ret;
        }

        function checkHit(img, x, y, now){
            var ret = false;
            var adj = now ? 10 : 0;
            if((img.x + adj) < x && x < (img.x + img.width - adj)
                    && (img.y + adj) < y && y < (img.y + img.height - adj)){
                ret = true;
            }
            return ret;
        }
    }
    //ハンドルを戻すアニメーション
    SequentialAnimation{
        id: _handleBackAnim
        running: false
        ParallelAnimation{
            NumberAnimation{
                target: _handle
                property: "x"
                to: _handle.default_x
                duration: 300
                easing.type: Easing.OutCubic
            }
            NumberAnimation{
                target: _handle
                property: "y"
                to: _handle.default_y
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
        ScriptAction{
            script: _handle.forcePressed = false
        }
    }

    ImageClock{
        y: 70 * viewScale
        height: 82 * viewScale
        anchors.right: parent.right
        anchors.rightMargin: 90 * viewScale

        clockNumberWidth: height
        clockColonWidth: 26 * viewScale
        spacing: 12 * viewScale

        numberPics: new Array("images/lock_num_l_0.png"
            , "images/lock_num_l_1.png"
            , "images/lock_num_l_2.png"
            , "images/lock_num_l_3.png"
            , "images/lock_num_l_4.png"
            , "images/lock_num_l_5.png"
            , "images/lock_num_l_6.png"
            , "images/lock_num_l_7.png"
            , "images/lock_num_l_8.png"
            , "images/lock_num_l_9.png"
                    )
        colonPic: "images/lock_num_l_c.png"
    }

    //外円とカメラとかアンロックとか
    Item{
        id: _funcItems
        anchors.fill: parent
        opacity: 0 //_handle.pressed ? 1.0 : 0.0
        //円
        Rectangle{
            id: _circle
            x: parent.width / 2 - width / 2
            y: _handle.default_y + 216 * 0.5 * viewScale - _handle.r
            width: _handle.r * 2
            height: width
            radius: width / 2
            color: "#00000000"
            opacity: 0.5
            border.color: "#ffffff"
            border.width: 2
        }
        Image{
            id: _camera
            x: _handle.default_x - _handle.r
            y: _handle.default_y
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_camera_normal.png"
        }
        Image{
            id: _silent
            x: _handle.default_x
            y: _handle.default_y - _handle.r
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_silent_normal.png"
        }
        Image{
            id: _unlock
            x: _handle.default_x + _handle.r
            y: _handle.default_y
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_unlock_normal.png"
        }
    }
    //外円のアニメーション
    NumberAnimation {
        id: _funcItemsAnim
        running: false
        target: _funcItems
        property: "opacity"
        to: 0
        duration: 1000
        easing.type: Easing.Linear
    }

    //ほわほわ
    LockScreenChevron{
        id: _chevron
    }


    //カレンダー
    LockScreenCalendar{
        x: 473 * viewScale
        y: 185 * viewScale
        numberPics: new Array("images/lock_num_s_0.png"
                    , "images/lock_num_s_1.png"
                    , "images/lock_num_s_2.png"
                    , "images/lock_num_s_3.png"
                    , "images/lock_num_s_4.png"
                    , "images/lock_num_s_5.png"
                    , "images/lock_num_s_6.png"
                    , "images/lock_num_s_7.png"
                    , "images/lock_num_s_8.png"
                    , "images/lock_num_s_9.png"
                    )
        weekdayPics: new Array("images/lock_week_sun.png"
                               , "images/lock_week_mon.png"
                               , "images/lock_week_tsu.png"
                               , "images/lock_week_wed.png"
                               , "images/lock_week_thu.png"
                               , "images/lock_week_fri.png"
                               , "images/lock_week_sat.png"
                               )
    }

    //充電不要
    Image{
        width: 102 * viewScale
        height: 24 * viewScale
        x: 525 * viewScale
        y: 220 * viewScale
        smooth: true
        source: "images/lock_charge_full.png"
    }

    // SIMなし
    Image{
        width: 542 * viewScale
        height: 28 * viewScale
        anchors.horizontalCenter: parent.horizontalCenter
        y: 1080 * viewScale
        smooth: true
        source: "images/lock_none_sim_message.png"
    }

}
